//
//  CBLDemoTests.m
//  CBLDemoTests
//
//  Created by ChenQi on 13-12-31.
//  Copyright (c) 2013年 Everimaging. All rights reserved.
//

#import <XCTest/XCTest.h>

#import "TestModel.h"
#import "CouchbaseLite.h"


#define kTEST_STR @"4234332423432432432432234234jlhglhlkhwelrhwethewjbg0000000000000////"

@interface CBLDemoTests : XCTestCase

@end

@implementation CBLDemoTests

- (void)setUp
{
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown
{
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testInsert
{
    NSLog(@"begin open db: %@", [NSDate date]);
    CBLDatabase *database = [[CBLManager sharedInstance] databaseNamed:@"test" error:NULL];
    XCTAssertNotNil(database, @"open database failed");
    NSLog(@"end open db: %@", [NSDate date]);
    
    static const NSUInteger count = 100;
    NSMutableArray *arry = [NSMutableArray arrayWithCapacity:count];
    NSLog(@"begin create model: %@", [NSDate date]);
    for (NSUInteger i = 0; i < count; ++i)
    {
        TestModel *model = [[TestModel alloc] initWithNewDocumentInDatabase:database];
        [model setValue:@"testModel" ofProperty: @"type"];


        model.test1 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test2 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test3 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test4 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test5 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test6 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test7 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test8 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test9 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];
        model.test10 = [NSString stringWithFormat:@"%@%ld", kTEST_STR, (unsigned long)i];

        model.str1 = [NSString stringWithFormat:@"%@", kTEST_STR];
        model.str2 = [NSString stringWithFormat:@"%@", kTEST_STR];
        model.str3 = [NSString stringWithFormat:@"%@", kTEST_STR];
        model.str4 = [NSString stringWithFormat:@"%@", kTEST_STR];
        model.str5 = [NSString stringWithFormat:@"%@", kTEST_STR];

        [arry addObject:model];

    }

    NSLog(@"end create model: %@", [NSDate date]);


    NSLog(@"begin save to db: %@", [NSDate date]);
    BOOL ret = [TestModel saveModels:arry error:NULL];
    NSLog(@"end save to db: %@", [NSDate date]);
    XCTAssertTrue(ret, @"save failed");
    
    CBLView* view = [database viewNamed: @"lists"];
    if (!view.mapBlock) {
        // Register the map function, the first time we access the view:
        [view setMapBlock: MAPBLOCK({
            if ([doc[@"type"] isEqualToString:@"testModel"])
                emit(doc[@"test1"], doc);
        }) version: @"1"]; // bump version any time you change the MAPBLOCK body!
        
        //        [view setMapBlock: MAPBLOCK({
        //            if ([doc[@"type"] isEqualToString:@"testModel"])
        //                emit(doc[@"test1"], doc);
        //        }) reduceBlock:REDUCEBLOCK({
        //            NSUInteger count = 0;
        //            if (rereduce)
        //            {
        //                for (NSNumber *num in values)
        //                {
        //                    count += num.unsignedIntegerValue;
        //                }
        //            }
        //            else
        //            {
        //                count = values.count;
        //            }
        //            return @(count);
        //        }) version: @"1"]; // bump version any time you change the MAPBLOCK body!
    }
    CBLQuery *query = [view createQuery];
    query.startKey = @"4234332423432432432432234234jlhglhlkhwelrhwethewjbg0000000000000////3";
    query.endKey = @"4234332423432432432432234234jlhglhlkhwelrhwethewjbg0000000000000////4";
    query.limit = 1000; // 只取一百条
    
    NSLog(@"begin fetch: %@", [NSDate date]);
    NSUInteger i = 0;
    for (CBLQueryRow *row in [query run:NULL])
    {
        if (i == 0)
        {
            NSLog(@"end fetch: %@", [NSDate date]);
        }
        ++i;
        //        NSLog(@"%@", row.value);
        //        TestModel *mode = [TestModel modelForDocument:row.document];
        //        mode.test2 = nil;
    }
    
    NSLog(@"%ld", (unsigned long)i);
    NSLog(@"end enumerate data: %@", [NSDate date]);
}


@end
